package br.ueg.ezrides.visao.composer;

import java.util.Map;

import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zul.Window;

import br.com.vexillum.control.util.Attachment;
import br.com.vexillum.util.ReflectionUtils;
import br.com.vexillum.util.Return;
import br.ueg.ezrides.visao.attachments.AttachmentMedia;
import br.ueg.ezrides.visao.validator.ImageValidator;

@SuppressWarnings("serial")
public class ProfileComposer extends UserComposer {

	public void doAfterCompose(Component comp) throws Exception{
		super.doAfterCompose(comp);	
		if(!(haveIdOnRequest() && initUserById(Executions.getCurrent().getParameter("id")))){
			Executions.sendRedirect("/paginas/erros/error.zul");
		} 
		loadBinder();
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void changeProfileImage(UploadEvent event){
        Media media = event.getMedia();
        ImageValidator val = new ImageValidator(media);
        Return ret = val.upload();
        if(ret.isValid()){
        		Attachment att = new AttachmentMedia();
                att.uploadAttachment(media, "image_profile", getUserInSession());
                //imageProfile.setContent((AImage) media);
                Executions.sendRedirect("");
        }
        treatReturn(ret);
	}
	
	public void deleteProfileImage(){
		@SuppressWarnings("rawtypes")
		Attachment att = new AttachmentMedia();
        Return ret = new Return(true);
        if(ret.isValid()){
                att.deleteAttachment("image_profile", getEntity());
                Executions.sendRedirect("");
        }
        treatReturn(ret);
}

	public void callModalWindow(String page){
		Map<String, Object> map = ReflectionUtils.prepareDataForPersistence(this);
		
		Component comp = Executions.createComponents(page, null, map);				
         
        if(comp instanceof Window) {
            ((Window)comp).doModal();
        }
	}
	
}
